package coffeebar;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class MoneyCountingArea extends QuestionPanel {

	JPanel moneyArea;
	String[] moneyImagePath = new String[] { "10pounds.jpg", "5pounds.jpg",
			"2pounds.jpg", "1pound.jpg", "50.jpg", "20.jpg", "10.jpg", "5.jpg",
			"2.jpg", "1.jpg" };
	double[] value = new double[] { 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02,
			0.01 };
	Money[] money = new Money[10];

	JLabel total;
	JLabel moneyLabel = new JLabel("Count the money in the till:");
	JButton submit;
	QuestionPanel totalArea;
	double realTotal;
	double inTill;

	public MoneyCountingArea() {
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		
		c.gridy = 0;
		c.gridwidth = 3;
		c.insets = new Insets(0,0,10,0);
		c.anchor = GridBagConstraints.LINE_START;
		this.add(moneyLabel,c);
		
		c.gridy = 1;
		c.gridwidth = 1;
		
		
		for (int i = 0; i < money.length; i++) {
			c.gridx = i;
			money[i] = new Money(new ImageIcon(TalkToDB.imagePath
					+ moneyImagePath[i]));
			money[i].setText("0");
			this.add(money[i],c);
		}
		
		total = new JLabel("Total: 0.00 (should be: " + realTotal + ")");
		submit = new JButton("submit");
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				submitMoney();
			}
		});

		submit.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int key = e.getKeyCode();
				if (key == KeyEvent.VK_ENTER) {
					submitMoney();
				}
			}
		});

		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 7;
		c.anchor = GridBagConstraints.LINE_START;
		this.add(total,c);
		
		c.gridx = 8;
		c.gridwidth = 3;
		c.insets = new Insets(10,0,0,0);
		c.anchor = GridBagConstraints.CENTER;
		this.add(submit,c);
		//totalArea.setLayout(new BorderLayout());
		
		//submit.setAlignmentX(Box.RIGHT_ALIGNMENT);
		
		// totalArea.setMaximumSize(new
		// Dimension(totalArea.getMaximumSize().width,submit.getPreferredSize().height));
		//moneyArea.setPreferredSize(new Dimension(money.length * 100, 120));
		//moneyArea.setMaximumSize(moneyArea.getMaximumSize());

		
		
//		moneyLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
//		moneyArea.setAlignmentX(Component.LEFT_ALIGNMENT);
//		totalArea.setAlignmentX(Component.LEFT_ALIGNMENT);
		//this.add(moneyLabel);
		//this.add(Box.createRigidArea(new Dimension(0, 5)));
		//this.add(moneyArea);
		//this.add(fake);
		//this.add(totalArea);
		
		
	}

	public void calculateTotal() {
		inTill = 0;
		for (int i = 0; i < money.length; i++) {
			try {
				inTill = inTill + Double.parseDouble(money[i].getText())
						* value[i];
			} catch (Exception e) {
			}
		}
		this.total.setText("Total cash in till: "
				+ new Pound(inTill).toString() + " (should be: " + realTotal
				+ ")");
	}

	public void update() {
		calculateTotal();
	}

	class Money extends JPanel {
		private JLabel image;
		private JTextField text;

		public Money(ImageIcon image) {
			this.image = new JLabel();
			text = new JTextField();

			this.image.setMaximumSize(new Dimension(100, 100));
			this.text.setMaximumSize(new Dimension(200, 20));
			this.setMaximumSize(new Dimension(100, 120));
			
			setImage(image);
			this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			this.add(this.image);
			this.add(text);
			  
			text.addKeyListener(new KeyListener() {
				public void keyTyped(KeyEvent e) {

				}

				public void keyReleased(KeyEvent e) {
					calculateTotal();
				}

				public void keyPressed(KeyEvent e) {
					int key = e.getKeyCode();
					if (key == KeyEvent.VK_ENTER) {
						text.transferFocus();
					}
				}
			});

			text.addFocusListener(new FocusListener() {

				public void focusGained(FocusEvent e) {
					text.setSelectionStart(0);
					text.setSelectionEnd(1000);
				}

				public void focusLost(FocusEvent e) {
					// do nothing
				}

			});
		}

		public void setImage(Icon image) {
			this.image.setIcon(image);
		}

		public void setText(String text) {
			this.text.setText(text);
		}

		public String getText() {
			return text.getText();
		}

		public Icon getImage() {
			return image.getIcon();
		}

		public void setEnabled(boolean en) {
			text.setEnabled(en);
			image.setEnabled(en);
		}
	}

	/*public void setGrayed(boolean en) {
		for (int i = 0; i < money.length; i++) {
			money[i].setEnabled(!en);
		}
		moneyLabel.setEnabled(!en);
	}*/

	private void submitMoney() {
		setGrayed(true);
		Controller.recordMoney(inTill);
	}
}
